/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * vwRegister.java
 *
 * Created on Apr 29, 2011, 6:12:05 PM
 */
package tcnplanner;

import javax.swing.JOptionPane;


/**
 *
 * @author Koen
 */
public class vwRegister extends javax.swing.JPanel {

    /** Creates new form vwRegister */
    public vwRegister() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        gpRegister = new tcnplanner.GradientPanel();
        btRegister = new javax.swing.JButton();
        tfUsername = new javax.swing.JTextField();
        lbLogin = new javax.swing.JLabel();
        lbPassword = new javax.swing.JLabel();
        btBack = new javax.swing.JButton();
        lbAdress = new javax.swing.JLabel();
        lbName = new javax.swing.JLabel();
        tfAdress = new javax.swing.JTextField();
        tfName = new javax.swing.JTextField();
        lbTelephone = new javax.swing.JLabel();
        tfTelephone = new javax.swing.JTextField();
        lbEmail = new javax.swing.JLabel();
        tfEmail = new javax.swing.JTextField();
        cbActorType = new javax.swing.JComboBox();
        lbRegister = new javax.swing.JLabel();
        lbActorType = new javax.swing.JLabel();
        tfPassword = new javax.swing.JPasswordField();

        gpRegister.setBackground(new java.awt.Color(153, 153, 153));
        gpRegister.setForeground(new java.awt.Color(255, 255, 255));
        gpRegister.setDirection(2);
        gpRegister.setName("gpRegister"); // NOI18N

        btRegister.setText("Register");
        btRegister.setName("btRegister"); // NOI18N
        btRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRegisterActionPerformed(evt);
            }
        });

        tfUsername.setName("tfUsername"); // NOI18N

        lbLogin.setText("Username:");
        lbLogin.setName("lbLogin"); // NOI18N

        lbPassword.setText("Password:");
        lbPassword.setName("lbPassword"); // NOI18N

        btBack.setText("Back");
        btBack.setName("btBack"); // NOI18N
        btBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBackActionPerformed(evt);
            }
        });

        lbAdress.setText("Adress:");
        lbAdress.setName("lbAdress"); // NOI18N

        lbName.setText("Name:");
        lbName.setName("lbName"); // NOI18N

        tfAdress.setName("tfAdress"); // NOI18N

        tfName.setName("tfName"); // NOI18N

        lbTelephone.setText("Telephone:");
        lbTelephone.setName("lbTelephone"); // NOI18N

        tfTelephone.setName("tfTelephone"); // NOI18N

        lbEmail.setText("Email:");
        lbEmail.setName("lbEmail"); // NOI18N

        tfEmail.setName("tfEmail"); // NOI18N

        cbActorType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Planner", "Extern Customer", "Engineer", "Operator", "Intern Customer" }));
        cbActorType.setName("cbActorType"); // NOI18N
        cbActorType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbActorTypeActionPerformed(evt);
            }
        });

        lbRegister.setFont(new java.awt.Font("Tahoma", 1, 24));
        lbRegister.setText("Register");
        lbRegister.setName("lbRegister"); // NOI18N

        lbActorType.setText("Actor type:");
        lbActorType.setName("lbActorType"); // NOI18N

        tfPassword.setText("jPasswordField1");
        tfPassword.setName("tfPassword"); // NOI18N

        javax.swing.GroupLayout gpRegisterLayout = new javax.swing.GroupLayout(gpRegister);
        gpRegister.setLayout(gpRegisterLayout);
        gpRegisterLayout.setHorizontalGroup(
            gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gpRegisterLayout.createSequentialGroup()
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(gpRegisterLayout.createSequentialGroup()
                        .addGap(98, 98, 98)
                        .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(lbLogin)
                                .addComponent(lbPassword)
                                .addGroup(gpRegisterLayout.createSequentialGroup()
                                    .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(lbAdress)
                                        .addComponent(lbName))
                                    .addGap(13, 13, 13)))
                            .addComponent(lbTelephone)
                            .addComponent(lbEmail)
                            .addComponent(lbActorType))
                        .addGap(39, 39, 39)
                        .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(tfEmail)
                                .addComponent(tfTelephone)
                                .addComponent(tfAdress, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(tfName, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                                .addComponent(cbActorType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(gpRegisterLayout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tfUsername)
                                    .addComponent(tfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))))
                    .addGroup(gpRegisterLayout.createSequentialGroup()
                        .addGap(153, 153, 153)
                        .addComponent(lbRegister))
                    .addGroup(gpRegisterLayout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(btBack)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btRegister)))
                .addContainerGap(125, Short.MAX_VALUE))
        );
        gpRegisterLayout.setVerticalGroup(
            gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(gpRegisterLayout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(lbRegister)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbLogin)
                    .addComponent(tfUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbPassword))
                .addGap(11, 11, 11)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbAdress))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfTelephone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbTelephone))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbActorType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbActorType))
                .addGap(18, 18, 18)
                .addGroup(gpRegisterLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btBack)
                    .addComponent(btRegister))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gpRegister, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(gpRegister, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void setScreen(java.awt.Component s) {
        java.awt.Component mainpl = null;
        if (this.getParent().getName().equals("plMainPanel")) {
            mainpl = this.getParent();
        } else {
            java.awt.Component p = this.getParent();
            while (p == null && p.getParent() != null) {
                p = p.getParent();
                if (p.getName().equals("plMainPanel")) {
                    mainpl = p;
                }
            }
        }
        if (mainpl != null) {
            javax.swing.JPanel p = (javax.swing.JPanel) mainpl;
            p.removeAll();
            //SCREEN TYPE INFO
            java.awt.Component screen = s;
            //END OF SCREEN TYPE INFO
            javax.swing.GroupLayout plMainPanelLayout = new javax.swing.GroupLayout(p);
            p.setLayout(plMainPanelLayout);
            plMainPanelLayout.setHorizontalGroup(
                    plMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(screen, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE));
            plMainPanelLayout.setVerticalGroup(
                    plMainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(screen, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE));
        }
    }

    private void btBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBackActionPerformed

        setScreen(new vwLogin());
    }//GEN-LAST:event_btBackActionPerformed

    private void btRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRegisterActionPerformed
        boolean added =false;
        Controller.ActorType type =Controller.ActorType.INTERNCUSTOMER;
        switch (this.cbActorType.getSelectedIndex()) {
            case 0:
                type =Controller.ActorType.PLANNER;
                                break;
            case 1:
                type =Controller.ActorType.EXTERNCUSTOMER;
                break;
            case 2:
                type =Controller.ActorType.ENGINEER;
                break;
            case 3:
                type = Controller.ActorType.OPERATOR;
                break;
            case 4:
                type =Controller.ActorType.INTERNCUSTOMER;
                break;
        }
                
               added = Main.controller.RegisterAccount(this.tfUsername.getText(), this.tfPassword.getText(), type,this.tfName.getText(),this.tfAdress.getText(),this.tfTelephone.getText(),this.tfEmail.getText());
               
        
        if(added) JOptionPane.showMessageDialog(null, "You are registered");
    }//GEN-LAST:event_btRegisterActionPerformed

    private void cbActorTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbActorTypeActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbActorTypeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBack;
    private javax.swing.JButton btRegister;
    private javax.swing.JComboBox cbActorType;
    private tcnplanner.GradientPanel gpRegister;
    private javax.swing.JLabel lbActorType;
    private javax.swing.JLabel lbAdress;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JLabel lbLogin;
    private javax.swing.JLabel lbName;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JLabel lbRegister;
    private javax.swing.JLabel lbTelephone;
    private javax.swing.JTextField tfAdress;
    private javax.swing.JTextField tfEmail;
    private javax.swing.JTextField tfName;
    private javax.swing.JPasswordField tfPassword;
    private javax.swing.JTextField tfTelephone;
    private javax.swing.JTextField tfUsername;
    // End of variables declaration//GEN-END:variables
}
